package com.xiaoshuidi.cloud.module.member.repository;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicListVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicUpdateStatusVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPageVO;
import com.xiaoshuidi.cloud.module.member.enums.StatusEnum;
import com.xiaoshuidi.cloud.module.member.enums.topics.TopicsStateEnum;
import com.xiaoshuidi.cloud.module.member.mapper.topics.MemberTopicsMapper;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopics;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberTopicsRepository extends ServiceImpl<MemberTopicsMapper, MemberTopics> {

    private final MemberTopicsMapper mapper;

    public Boolean create(TopicsCreateVO createReqVO) {
        MemberTopics memberTopics = new MemberTopics();
        BeanUtils.copyProperties(createReqVO, memberTopics);
        mapper.insert(memberTopics);
        return true;
    }

    public Boolean edit(TopicsCreateVO createReqVO) {
        MemberTopics memberTopics = new MemberTopics();
        BeanUtils.copyProperties(createReqVO, memberTopics);
        mapper.updateById(memberTopics);
        return true;
    }

    public PageResult<TopicListVO> getPage(TopicsPageVO pageVO) {
        LambdaQueryWrapperX<MemberTopics> wrapper = generatorWrapper(pageVO);
        Page<MemberTopics> page = mapper.selectPage(new Page<MemberTopics>(pageVO.getPageNo(), pageVO.getPageSize()), wrapper);
        List<TopicListVO> collect = page.getRecords().stream().map(t -> {
            TopicListVO vo = new TopicListVO();
            BeanUtils.copyProperties(t, vo);
            if (ObjectUtils.isNotEmpty(pageVO.getTopicStatus())) {
                vo.setTopicStatus(pageVO.getTopicStatus());
            } else {
                vo.setTopicStatus(getTopicStatus(t));
            }
            return vo;
        }).collect(Collectors.toList());
        PageResult<TopicListVO> pageResult = PageResult.empty();
        pageResult.setList(collect);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public Integer getTopicStatus(MemberTopics t) {
        LocalDateTime now = LocalDateTime.now();
        //未生效 生效时间>当前时间
        if (t.getEffectBeginTime().isAfter(now)) {
            return StatusEnum.NOT_EFFECTIVE.getCode();
        } else if ((t.getEffectEndTime().isAfter(now) && now.isAfter(t.getEffectBeginTime())) || t.getEffectBeginTime() == now) {
            //已生效 生效时间<=当前时间<过期时间
            return StatusEnum.EFFECTIVE.getCode();
        } else {
            return StatusEnum.EXPIRED.getCode();
        }
    }

    private LambdaQueryWrapperX<MemberTopics> generatorWrapper(TopicsPageVO pageVO) {

        LambdaQueryWrapperX<MemberTopics> wrapperX = new LambdaQueryWrapperX<MemberTopics>()
                .geIfPresent(MemberTopics::getCreateTime, pageVO.getBeginTime())
                .leIfPresent(MemberTopics::getCreateTime, pageVO.getEndTime())
                .likeIfPresent(MemberTopics::getName, pageVO.getName())
                .orderByDesc(MemberTopics::getWeight)
                .orderByDesc(MemberTopics::getCreateTime);

        if (pageVO.getTopicStatus() != null && pageVO.getTopicStatus().intValue() == TopicsStateEnum.TO_BE_EFFECTIVE.getCode().intValue()) {
            //未生效 开始时间>当前时间
            wrapperX.gt(MemberTopics::getEffectBeginTime, LocalDateTime.now());
        } else if (pageVO.getTopicStatus() != null && pageVO.getTopicStatus().intValue() == StatusEnum.EFFECTIVE.getCode().intValue()) {
            //已生效 开效时间<=当前时间<结束时间
            wrapperX.le(MemberTopics::getEffectBeginTime, LocalDateTime.now())
                    .gt(MemberTopics::getEffectEndTime, LocalDateTime.now());
        } else if (pageVO.getTopicStatus() != null && pageVO.getTopicStatus().intValue() == StatusEnum.EXPIRED.getCode().intValue()) {
            //已失效   过期时间<=当前时间
            wrapperX.le(MemberTopics::getEffectEndTime, LocalDateTime.now());
        }
        return wrapperX;
    }

    public IPage<MemberTopics> getAppPage(TopicsPageVO pageVO) {
        //话题是启用状态且在有效期之内
        LambdaQueryWrapper<MemberTopics> wrapper = new LambdaQueryWrapper<MemberTopics>()
//                .le(MemberTopics::getEffectBeginTime, LocalDateTime.now())
//                .gt(MemberTopics::getEffectEndTime, LocalDateTime.now())
                .eq(MemberTopics::getStatus, true)
                .orderByDesc(MemberTopics::getWeight)
                .orderByDesc(MemberTopics::getCreateTime);
        return mapper.selectPage(new Page<MemberTopics>(pageVO.getPageNo(), pageVO.getPageSize()), wrapper);
    }

    public Boolean updateTopicStatus(TopicUpdateStatusVO topicUpdateStatusVO) {
        MemberTopics memberTopics = new MemberTopics();
        memberTopics.setId(topicUpdateStatusVO.getId());
        memberTopics.setStatus(topicUpdateStatusVO.getStatus());
        int result = mapper.updateById(memberTopics);
        if (result == 1) {
            return true;
        }
        return false;

    }
}
